//
//  UIImage+blur.m
//  Stars
//
//  Created by 魏鹏 on 16/11/14.
//  Copyright © 2016年 hst. All rights reserved.
//

#import "UIImage+blur.h"

@implementation UIImage (blur)
+(UIImage *)coreBlurImage:(UIImage *)image
           withBlurNumber:(CGFloat)blur {

//    CIContext *context = [CIContext contextWithOptions:nil];
//    CIImage *inputImage= [CIImage imageWithCGImage:image.CGImage];
//    //设置filter
//    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
//    [filter setValue:inputImage forKey:kCIInputImageKey];
//    [filter setValue:@(blur) forKey: @"inputRadius"];
//    //模糊图片
//    CIImage *result=[filter valueForKey:kCIOutputImageKey];
////    CGImageRef outImage=[context createCGImage:result fromRect:[result extent]];
//    CGImageRef outImage = [context createCGImage: result fromRect:[inputImage extent]];
//    UIImage *blurImage=[UIImage imageWithCGImage:outImage];
//    CGImageRelease(outImage);
//    return blurImage;
    
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *affineClampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    CGAffineTransform xform = CGAffineTransformMakeScale(1.0, 1.0);
    [affineClampFilter setValue:inputImage forKey:kCIInputImageKey];
    [affineClampFilter setValue:[NSValue valueWithBytes:&xform
                                               objCType:@encode(CGAffineTransform)]
                         forKey:@"inputTransform"];
    CIImage *extendedImage = [affineClampFilter valueForKey:kCIOutputImageKey];
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blurFilter setValue:extendedImage forKey:kCIInputImageKey];
    [blurFilter setValue:[NSNumber numberWithFloat:10.0f] forKey:@"inputRadius"];
    CIImage *result = [blurFilter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
    UIImage *returnImage = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    
    return returnImage;

}

+(UIImage *)imageByApplyingAlpha:(CGFloat )alpha  image:(UIImage*)image
{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    
    CGContextScaleCTM(ctx, 1, -1);
    
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    
    CGContextSetAlpha(ctx, alpha);
    
    
    
    CGContextDrawImage(ctx, area, image.CGImage);
    
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    
    UIGraphicsEndImageContext();
    
    
    
    return newImage;
    
}
@end
